/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: GodK
 * Date: 2024-03-23
 * Time: 21:40
 */
// 1、无头单向非循环链表实现
public class MyHeadLesssingleList {
    static class ListNode{
        public int data;
        public ListNode next;

        public ListNode(int data) {
            this.data = data;
        }

    }
    public ListNode head;
    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next=head;
        head=node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head==null){
            return;
        }
        ListNode cur = head;
        while (cur.next!=null){
            cur=cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        try {
            checkIndex(index);
        }catch (IndexNotlegalException e){
            e.printStackTrace();
        }
        ListNode node = new ListNode(data);
        if(index==0){
            addFirst(data);
            return;
        }
        if (index==size()){
            addLast(data);
            return;
        }
        //ListNode cur = ListNode cur =head;
        ListNode cur = head;
        for (int i = 0 ; i<index ; i++){
            cur=cur.next;
        }
        node.next=cur.next;
        cur.next=node;
    };
    private void checkIndex(int index) throws IndexNotlegalException{
        if(index<0||index>size()) {
            throw new IndexNotlegalException("index不合法");
        }
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur!=null){
            if(cur.data==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    };
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (head==null)return;
        if(head.data == key) {
            head = head.next;
            return;
        }
        ListNode cur = head.next;
        ListNode prev = head;
        while (cur!=null){
            if(cur.data==key){
                prev.next=cur.next;
            }
            cur=cur.next;
            prev=prev.next;
        }
    };
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if (head==null)return;
        ListNode cur = head;
        while (cur.next!=null){
            if(cur.next.data==key){
                cur.next=cur.next.next;
            }else {
                cur=cur.next;
            }
        }
        if(head.data==key){
            head=head.next;
        }
    };
    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while (cur.next!=null){
            cur=cur.next;
            count++;
        }
        return count;
    };
    public void display(){
        ListNode cur = head;
        while (cur!=null){
            System.out.println(cur.data+" ");
            cur=cur.next;
        }
        System.out.println();
    };
    public void clear(){
        head=null;
    }
    public ListNode middleNode(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}
